* Table A2: Sample Restrictions

clear
use "${data_dir}DavisMazumderData.dta"


matrix tableA2 = J(5,2,.)

* Full Sample
count if survey==1966
matrix tableA2[1,1] = r(N)
count if survey==1979
matrix tableA2[1,2] = r(N)



* Create a flag for still being in our sample after sample restriction
gen keep = age_firstSurvey<=18 

* Sample sizes after <=18 at first survey restriction
count if keep==1 & survey==1966
matrix tableA2[2,1] = r(N)
count if keep==1 & survey==1979
matrix tableA2[2,2] = r(N)

*Parent out of Range
count if keep==1 & !((mom_outOfRange==1 & linkMom==1 & !(dad_outOfRange==0 & linkDad==1)) | (dad_outOfRange==1 & linkDad==1 & !(mom_outOfRange==0 & linkMom==1)) | (linkMom==0 & linkDad==0) ) & survey==1979


* Update flag after applying parent out of range restriction
replace keep = (keep==1 & !((mom_outOfRange==1 & linkMom==1 & !(dad_outOfRange==0 & linkDad==1)) | (dad_outOfRange==1 & linkDad==1 & !(mom_outOfRange==0 & linkMom==1)) | (linkMom==0 & linkDad==0) ))
 
 * Sample sizes after out of range restriction
count if keep==1 & survey==1966
matrix tableA2[3,1] = r(N)
count if keep==1 & survey==1979
matrix tableA2[3,2] = r(N)

 
 * Missing Income restriction
count if keep==1 & miss0==0 & survey==1966  // Parent incom
matrix tableA2[4,1] = r(N)
count if keep==1 & miss0==0 & miss1==0 & survey==1966 // Parent and child income
matrix tableA2[5,1] = r(N)
 
count if keep==1 & miss0==0 & survey==1979 // Parent income
matrix tableA2[4,2] = r(N)
count if keep==1 & miss0==0 & miss1==0 & survey==1979 // Parent and child income
matrix tableA2[5,2] = r(N)
 
clear
svmat tableA2

gen change66 = tableA21[_n-1]-tableA21 
gen change79 = tableA22[_n-1]-tableA22

gen label = ""
 replace label = "Full Sample Size" in 1
 replace label = "18 or younger at first survey" in 2
 replace label = "In NLS66 Sampling Frame" in 3
 replace label = "Not Missing Parent Income" in 4
 replace label = "Not Missing Child Income" in 5
 

outsheet label tableA21 change66 tableA22 change79 using ${results_dir}tableA2.csv, comma names replace

exit
